import $service from '../../../../service.js';
var that;
var this_;
const app = getApp();
let page = 1;
Page({
    data:{
        doctorList:[],
        departmentList:[],
        doctorName:'',
        departmentId:'',
        isLoginshow:false,
        departmentName: '',
        onMore:false,
        isFee: false,
        feeData: null,
    },
    onShow(){
        page = 1;
        that = this.data;
        this_ = this;
        wx.hideHomeButton()
        this.getDepartmentListPc()
        this.getHomeDoctorList();
        this.socketInit();
        this.selectComponent("#doctorList").getConsultationFee()
    },
    socketInit() {
        app.webSocket.initWebSocket({ url: $service.wsUrl})
        app.webSocket.onReceivedMsg(res => {
          console.log(res)
          if (res.packageType == 12) {
            this.getHomeDoctorList();
          }
        })
      },
     // 获取科室字典
     getDepartmentListPc() {
        app.http.getDepartmentListPc().then(res => {
            this.setData({
                departmentList: res.data.list
            })
        })
    },
    // 获取医生列表
    getHomeDoctorList() {
        let params = {
            "current": page,
            "departmentId": that.departmentId,
            "keyWord": that.doctorName,
            "size": 10
          }
        app.http.getHomeDoctorList(params, true).then(res => {
            let list = $service.deepClone(res.data.list)
            if (that.noMore) return
            if (res.data.total < 10) {
                this.setData({
                    noMore: true
                })
            }
            if (page != 1) {
                list = that.doctorList.concat(list)
            }
            list.forEach((item, index) => {
                if (item.isOnline === 1) {
                    list.splice(index, 1);
                    list.unshift(item)
                }
            })
            this.setData({
                doctorList: list,
            })
        }).catch(err => {
            $service.showToast(err.msg)
        }).finally(() => {
            wx.stopPullDownRefresh()
        })
    },
    //监听input框
    inputChange(e){
        page = 1;
        this.setData({
            doctorName : e.detail.value
        })
        this.getHomeDoctorList()
    },
    //科室选择
    departmentChange(e) {
        let index = e.detail.value;
        page = 1;
        this.setData({
            noMore: false,
            departmentId: that.departmentList[index].id,
            departmentName: that.departmentList[index].departmentName
        })
        this.getHomeDoctorList()
    },
    onPullDownRefresh() {
        this.setData({
            noMore: false
        })
        page = 1;
        this.getHomeDoctorList();
    },
    onReachBottom() {
        if (!that.onMore) {
            page ++
            this.getHomeDoctorList();
        }
    },
})